package com.common.tools;

import java.io.UnsupportedEncodingException;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * 3DES加密工具 <功能详细描述>
 * 
 * @author chenlujun
 * @version [版本号, 2014-11-3]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class ThreeDesTools {

	// 定义加密算法，有DES、DESede(即3DES)、Blowfish
	private static final String Algorithm = "DESede"; 

	/**
	 * 加密字符串 
	 * 
	 * @param key
	 *            加密秘钥
	 * @param src
	 *            加密前字符串
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static String encryptMode(String key, String src) {
		try {

			byte[] keybyte = build3DesKey(key);
			byte[] srcbyte = src.getBytes("UTF8");
			// 生成密钥
			SecretKey deskey = new SecretKeySpec(keybyte, Algorithm); // 加密
			Cipher c1 = Cipher.getInstance(Algorithm);
			c1.init(Cipher.ENCRYPT_MODE, deskey);
			byte[] byteMi = c1.doFinal(srcbyte);

			return StringTools.bytesToHexString(byteMi);
			
		} catch (java.security.NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e2) {
			e2.printStackTrace();
		} catch (java.lang.Exception e3) {
			e3.printStackTrace();
		}
		return null;
	}

	/**
	 * 解密字符串 
	 * 
	 * @param key
	 *            秘钥
	 * @param src
	 *            需要解密字符串
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static String decryptMode(String key, String src) {
		try {
			
			byte[] keybyte = build3DesKey(key);
			byte[] srcbyte = StringTools.hexStringToBytes(src);
			// 生成密钥
			SecretKey deskey = new SecretKeySpec(keybyte, Algorithm); // 解密
			Cipher c1 = Cipher.getInstance(Algorithm);
			c1.init(Cipher.DECRYPT_MODE, deskey);
			byte[] byteMing = c1.doFinal(srcbyte);
			String strMing = new String(byteMing, "UTF8");
			return strMing;
		} catch (java.security.NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e2) {
			e2.printStackTrace();
		} catch (java.lang.Exception e3) {
			e3.printStackTrace();
		}
		return null;
	}

	/*
	 * 根据字符串生成密钥字节数组
	 * 
	 * @param keyStr 密钥字符串
	 * 
	 * @return
	 * 
	 * @throws UnsupportedEncodingException
	 */
	private static byte[] build3DesKey(String keyStr) {
		byte[] key = new byte[24]; // 声明一个24位的字节数组，默认里面都是0
		byte[] temp;
		try {
			temp = keyStr.getBytes("UTF8");

			// 将字符串转成字节数组
			/*
			 * 执行数组拷贝 System.arraycopy(源数组，从源数组哪里开始拷贝，目标数组，拷贝多少位)
			 */
			if (key.length > temp.length) {
				// 如果temp不够24位，则拷贝temp数组整个长度的内容到key数组中
				System.arraycopy(temp, 0, key, 0, temp.length);
			} else {
				// 如果temp大于24位，则拷贝temp数组24个长度的内容到key数组中
				System.arraycopy(temp, 0, key, 0, key.length);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		return key;
	}

	public static void main(String[] args) {

		String key = "FAF7D5D92ACA08B69A787185";
		StringBuffer sb=new StringBuffer();
		String msg = "{\"userId\":\"39\",\"locationId\":\"820139\"}";
		sb.append(msg);
		
		System.out.println(sb.length());
		System.out.println("【加密前】：" + sb.toString());
		String secretArr = ThreeDesTools.encryptMode(key, sb.toString());
		System.out.println("【加密后】：" + secretArr);
		String ddd = ThreeDesTools.decryptMode(key, secretArr);
		System.out.println("【解密后】：" + ddd);
	}
	
}
